// 处理头像预览
function previewAvatar(event) {
    const file = event.target.files[0];
    if (file) {
        const reader = new FileReader();
        reader.onload = function(e) {
            document.getElementById('avatarPreview').src = e.target.result;
        };
        reader.readAsDataURL(file);
    }
}

// 更新个人信息
function updateProfile(event) {
    event.preventDefault();
    const form = document.getElementById('profileForm');
    const formData = new FormData(form);

    fetch('/api/profile/update', {
        method: 'POST',
        body: formData
    })
    .then(response => response.json())
    .then(data => {
        if (data.success) {
            alert('个人信息更新成功！');
            // 刷新页面以显示更新后的信息
            window.location.reload();
        } else {
            alert(data.message || '更新失败，请稍后重试');
        }
    })
    .catch(error => {
        console.error('更新个人信息出错:', error);
        alert('更新过程中出现错误，请稍后重试');
    });
}

// 修改密码
function changePassword(event) {
    event.preventDefault();
    const form = document.getElementById('changePasswordForm');
    const formData = new FormData(form);

    // 验证新密码和确认密码是否匹配
    const newPassword = formData.get('new_password');
    const confirmPassword = formData.get('confirm_password');
    
    if (newPassword !== confirmPassword) {
        alert('新密码和确认密码不匹配');
        return;
    }

    fetch('/api/profile/change-password', {
        method: 'POST',
        body: JSON.stringify({
            current_password: formData.get('current_password'),
            new_password: newPassword
        }),
        headers: {
            'Content-Type': 'application/json'
        }
    })
    .then(response => response.json())
    .then(data => {
        if (data.success) {
            alert('密码修改成功！');
            form.reset();
        } else {
            alert(data.message || '密码修改失败，请稍后重试');
        }
    })
    .catch(error => {
        console.error('修改密码出错:', error);
        alert('修改过程中出现错误，请稍后重试');
    });
}

// 页面加载完成后初始化
document.addEventListener('DOMContentLoaded', function() {
    // 为头像上传添加事件监听
    const avatarInput = document.getElementById('avatarInput');
    if (avatarInput) {
        avatarInput.addEventListener('change', previewAvatar);
    }

    // 为个人信息表单添加提交事件监听
    const profileForm = document.getElementById('profileForm');
    if (profileForm) {
        profileForm.addEventListener('submit', updateProfile);
    }

    // 为密码修改表单添加提交事件监听
    const changePasswordForm = document.getElementById('changePasswordForm');
    if (changePasswordForm) {
        changePasswordForm.addEventListener('submit', changePassword);
    }

    // 加载用户信息
    fetch('/api/profile/info')
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                const userData = data.data;
                // 填充表单数据
                document.getElementById('nickname').value = userData.nickname || '';
                document.getElementById('email').value = userData.email || '';
                document.getElementById('phone').value = userData.phone || '';
                if (userData.avatar) {
                    document.getElementById('avatarPreview').src = userData.avatar;
                }
            }
        })
        .catch(error => {
            console.error('加载用户信息出错:', error);
        });
}); 